import Vue from "vue";
import VueRouter from "vue-router";
// import Home from "../views/Home";
// import Login from "../views/Login";
// import Register from "../views/Register";
// import Search from "../views/Search";
// import Detail from "../views/Detail";
// import AddCartSuccess from "../views/AddCartSuccess";
// import ShopCart from "../views/ShopCart";
// import Center from "../views/Center";
// import Pay from "../views/Pay";
// import PaySuccess from "../views/PaySuccess";
// import Trade from "../views/Trade";

//按需加载路由（路由懒加载）
const Home = () => import("../views/Home");
const Login = () => import("../views/Login");
const Register = () => import("../views/Register");
const Search = () => import("../views/Search");
const Detail = () => import("../views/Detail");
const AddCartSuccess = () => import("../views/AddCartSuccess");
const ShopCart = () => import("../views/ShopCart");
const Center = () => import("../views/Center");
const Pay = () => import("../views/Pay");
const PaySuccess = () => import("../views/PaySuccess");
const Trade = () => import("../views/Trade");

Vue.use(VueRouter);

const routes = [
	{
		name: "Home",
		path: "/",
		component: Home
	},
	{
		name: "Login",
		path: "/login",
		component: Login,
		meta: {
			isFooterHide: true
		}
	},
	{
		name: "Register",
		path: "/register",
		component: Register,
		meta: {
			isFooterHide: true
		}
	},
	{
		name: "Search",
		path: "/search/:keyword?",
		component: Search
	},
	{
		name: "Detail",
		path: "/detail",
		component: Detail
	},
	{
		name: "AddCartSuccess",
		path: "/addcartsuccess",
		component: AddCartSuccess
	},
	{
		name: "ShopCart",
		path: "/shopcart",
		component: ShopCart
	},
	{
		name: "Center",
		path: "/center",
		component: Center
	},
	{
		name: "Pay",
		path: "/pay",
		component: Pay
		/* beforeEnter(to, from, next) {
			if (from.name === "Trade") {
				next();
			} else {
				next({name:"Home"});
			}
		} */
	},
	{
		name: "PaySuccess",
		path: "/paysuccess",
		component: PaySuccess
		/* beforeEnter(to, from, next) {
			if (from.name === "Pay") {
				next();
			} else {
				next({name:"Home"});
			}
		} */
	},
	{
		name: "Trade",
		path: "/trade",
		component: Trade
		/* beforeEnter(to, from, next) {
			if (from.name === "ShopCart") {
				next();
			} else {
				next({name:"Home"});
			}
		} */
	}
];

const router = new VueRouter({
	mode: "history",
	base: process.env.BASE_URL,
	routes,
	scrollBehavior() {
		// return 期望滚动到哪个的位置
		return {
			a: 0,
			y: 0
		};
	}
});

export default router;
